<template>
  <el-dialog
    :title="!dataForm.rentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapId">
      <el-input v-model="dataForm.mapId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="租房方式(1.整租 2.合租)" prop="rentMode">
      <el-input v-model="dataForm.rentMode" placeholder="租房方式(1.整租 2.合租)"></el-input>
    </el-form-item>
    <el-form-item label="租房租金" prop="rentPrice">
      <el-input v-model="dataForm.rentPrice" placeholder="租房租金"></el-input>
    </el-form-item>
    <el-form-item label="租房房型(1.一室 2.二室 3.三室)" prop="rentRoom">
      <el-input v-model="dataForm.rentRoom" placeholder="租房房型(1.一室 2.二室 3.三室)"></el-input>
    </el-form-item>
    <el-form-item label="租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)" prop="rentOrient">
      <el-input v-model="dataForm.rentOrient" placeholder="租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)"></el-input>
    </el-form-item>
    <el-form-item label="租房租期(1.月租 2.季租 3.年租)" prop="rentTerm">
      <el-input v-model="dataForm.rentTerm" placeholder="租房租期(1.月租 2.季租 3.年租)"></el-input>
    </el-form-item>
    <el-form-item label="租房楼层(1.低楼层 2.中楼层 3.高楼层)" prop="rentFloor">
      <el-input v-model="dataForm.rentFloor" placeholder="租房楼层(1.低楼层 2.中楼层 3.高楼层)"></el-input>
    </el-form-item>
    <el-form-item label="租房特色(1.近地铁 2.环境好 3.低价)" prop="rentFeat">
      <el-input v-model="dataForm.rentFeat" placeholder="租房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="租房图片" prop="rentImg">
      <el-input v-model="dataForm.rentImg" placeholder="租房图片"></el-input>
    </el-form-item>
    <el-form-item label="租房简介" prop="rentIntro">
      <el-input v-model="dataForm.rentIntro" placeholder="租房简介"></el-input>
    </el-form-item>
    <el-form-item label="租房状态" prop="rentStatus">
      <el-input v-model="dataForm.rentStatus" placeholder="租房状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentId: 0,
          mapId: '',
          rentMode: '',
          rentPrice: '',
          rentRoom: '',
          rentOrient: '',
          rentTerm: '',
          rentFloor: '',
          rentFeat: '',
          rentImg: '',
          rentIntro: '',
          rentStatus: ''
        },
        dataRule: {
          mapId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          rentMode: [
            { required: true, message: '租房方式(1.整租 2.合租)不能为空', trigger: 'blur' }
          ],
          rentPrice: [
            { required: true, message: '租房租金不能为空', trigger: 'blur' }
          ],
          rentRoom: [
            { required: true, message: '租房房型(1.一室 2.二室 3.三室)不能为空', trigger: 'blur' }
          ],
          rentOrient: [
            { required: true, message: '租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)不能为空', trigger: 'blur' }
          ],
          rentTerm: [
            { required: true, message: '租房租期(1.月租 2.季租 3.年租)不能为空', trigger: 'blur' }
          ],
          rentFloor: [
            { required: true, message: '租房楼层(1.低楼层 2.中楼层 3.高楼层)不能为空', trigger: 'blur' }
          ],
          rentFeat: [
            { required: true, message: '租房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          rentImg: [
            { required: true, message: '租房图片不能为空', trigger: 'blur' }
          ],
          rentIntro: [
            { required: true, message: '租房简介不能为空', trigger: 'blur' }
          ],
          rentStatus: [
            { required: true, message: '租房状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentId) {
            this.$http({
              url: this.$http.adornUrl(`/house/renthouse/info/${this.dataForm.rentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapId = data.rentHouse.mapId
                this.dataForm.rentMode = data.rentHouse.rentMode
                this.dataForm.rentPrice = data.rentHouse.rentPrice
                this.dataForm.rentRoom = data.rentHouse.rentRoom
                this.dataForm.rentOrient = data.rentHouse.rentOrient
                this.dataForm.rentTerm = data.rentHouse.rentTerm
                this.dataForm.rentFloor = data.rentHouse.rentFloor
                this.dataForm.rentFeat = data.rentHouse.rentFeat
                this.dataForm.rentImg = data.rentHouse.rentImg
                this.dataForm.rentIntro = data.rentHouse.rentIntro
                this.dataForm.rentStatus = data.rentHouse.rentStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/renthouse/${!this.dataForm.rentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentId': this.dataForm.rentId || undefined,
                'mapId': this.dataForm.mapId,
                'rentMode': this.dataForm.rentMode,
                'rentPrice': this.dataForm.rentPrice,
                'rentRoom': this.dataForm.rentRoom,
                'rentOrient': this.dataForm.rentOrient,
                'rentTerm': this.dataForm.rentTerm,
                'rentFloor': this.dataForm.rentFloor,
                'rentFeat': this.dataForm.rentFeat,
                'rentImg': this.dataForm.rentImg,
                'rentIntro': this.dataForm.rentIntro,
                'rentStatus': this.dataForm.rentStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
